package com.lineage.server.timecontroller.pet;

import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldSummons;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SummonTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(SummonTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 60000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 60000L, 60000L);
  }
  
  public void run() {
    try {
      Collection<L1SummonInstance> allPet = WorldSummons.get().all();
      if (allPet.isEmpty())
        return; 
      Iterator<L1SummonInstance> iter = allPet.iterator();
      while (iter.hasNext()) {
        L1SummonInstance summon = iter.next();
        int time = summon.get_time() - 60;
        if (time <= 0) {
          outSummon(summon);
        } else {
          summon.set_time(time);
        } 
        Thread.sleep(50L);
      } 
    } catch (Exception e) {
      _log.error("召唤兽处理时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      SummonTimer summon_Timer = new SummonTimer();
      summon_Timer.start();
    } 
  }
  
  private static void outSummon(L1SummonInstance summon) {
    try {
      if (summon != null) {
        if (summon.destroyed())
          return; 
        summon.Death(null);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private static void Summon(L1PcInstance pc) {
    int[] summons = { 81210, 81213, 81216, 81219, 81222, 81225, 81228 };
    int summonid = 0;
    int summoncost = 8;
    int levelRange = 32;
    int i = 0;
    while (i < summons.length) {
      if (i == summons.length - 1) {
        summonid = summons[i];
        break;
      } 
      levelRange += 4;
      i++;
    } 
    int petcost = 0;
    Object[] petlist = pc.getPetList().values().toArray();
    Object[] array;
    int length = (array = petlist).length;
    int k = 0;
    while (k < length) {
      Object pet = array[k];
      petcost += ((L1NpcInstance)pet).getPetcost();
      k++;
    } 
    int pcCha = pc.getCha();
    if (pcCha > 34)
      pcCha = 34; 
    int charisma = pcCha + 6 - petcost;
    int summoncount = charisma / 8;
    L1Npc npcTemp = NpcTable.get().getTemplate(summonid);
    int j = 0;
    while (j < summoncount) {
      L1SummonInstance summon1 = new L1SummonInstance(npcTemp, (L1Character)pc);
      summon1.setPetcost(8);
      j++;
    } 
    pc.setSummonMonster(false);
  }
}
